<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="迎着朝阳的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    nginx学习之应用实践 |  迎着朝阳
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="https://dxysun.com/static/yan.png" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?aa994a8d65700b8835787dd39d079d7e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-nginxForLearn2"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  nginx学习之应用实践
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/01/05/nginxForLearn2/" class="article-date">
  <time datetime="2020-01-05T09:44:02.000Z" itemprop="datePublished">2020-01-05</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">12 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>nginx相关实践应用</p>
<a id="more"></a>

<h2 id="反向代理"><a href="#反向代理" class="headerlink" title="反向代理"></a>反向代理</h2><p>nginx反向代理的指令不需要新增额外的模块，默认自带proxy_pass指令，只需要修改配置文件就可以实现反向代理。<br>proxy_pass 既可以是ip地址，也可以是域名，同时还可以指定端口。<br>示例配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    server_name localhost;</span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">        proxy_pass http:&#x2F;&#x2F;192.168.11.161:8080;  # 代理服务地址</span><br><span class="line">        proxy_set_header Host $host;</span><br><span class="line">        proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="负载均衡"><a href="#负载均衡" class="headerlink" title="负载均衡"></a>负载均衡</h2><p>网络负载均衡的原理是利用一定的分配策略将网络负载平衡地分摊到网络集群的各个操作单元上，使得单个重负载任务能够分担到多个单元上并行处理，使得大量并发访问或数据流量分担到多个单元上分别处理，从而减少用户的等待响应时间。</p>
<h3 id="upstream"><a href="#upstream" class="headerlink" title="upstream"></a>upstream</h3><p>upstream 是Nginx的HTTP Upstream模块，这个模块通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡<br>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">upstream &#123;</span><br><span class="line">    server address [paramters];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>address 反向服务地址加端口</p>
</blockquote>
<p>upstream 参数</p>
<table>
<thead>
<tr>
<th align="center">参数</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">weight</td>
<td align="center">权重</td>
</tr>
<tr>
<td align="center">max_fails</td>
<td align="center">失败多少次 认为主机已挂掉则，踢出</td>
</tr>
<tr>
<td align="center">fail_timeout</td>
<td align="center">踢出后重新探测时间</td>
</tr>
<tr>
<td align="center">backup</td>
<td align="center">备用服务</td>
</tr>
<tr>
<td align="center">max_conns</td>
<td align="center">允许最大连接数</td>
</tr>
<tr>
<td align="center">slow_start</td>
<td align="center">当节点恢复，不立即加入</td>
</tr>
</tbody></table>
<p>示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">upstream tomcat &#123;</span><br><span class="line">    server 192.168.11.161:8080 max_fails&#x3D;2 fail_timeout&#x3D;60s;</span><br><span class="line">    server 192.168.11.159:8080;</span><br><span class="line">    ip_hash;  # 负载均衡算法</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    server_name localhost;</span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">        proxy_pass http:&#x2F;&#x2F;tomcat;</span><br><span class="line">        proxy_set_header Host $host;</span><br><span class="line">        proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">        proxy_next_upstream error timeout http_500 http_503;</span><br><span class="line">        proxy_connect_timeout 60s;</span><br><span class="line">        proxy_send_timeout 60s;</span><br><span class="line">        proxy_read_timeout 60s;</span><br><span class="line">        add_header &#39;Access-Control-Allow-Origin&#39; &#39;*&#39;;</span><br><span class="line">        add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET,POST,DELETE&#39;;</span><br><span class="line">        add_header &#39;Aceess-Control-Allow-Header&#39; &#39;Content-Type,*&#39;;</span><br><span class="line">    &#125;</span><br><span class="line">    location ~ .*\.(js|css|png|svg|ico|jpg)$ &#123;</span><br><span class="line">        valid_referers none blocked 192.168.11.160;</span><br><span class="line">        if ($invalid_referer) &#123;</span><br><span class="line">            return 404;</span><br><span class="line">        &#125;</span><br><span class="line">        root static-resource;</span><br><span class="line">        expires 1d;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>负载均衡策略或者算法<br>轮询算法（默认）， 如果后端服务器宕机以后，会自动踢出<br>ip_hash 根据请求的ip地址进行hash<br>权重轮询</p>
</blockquote>
<h2 id="其他配置信息"><a href="#其他配置信息" class="headerlink" title="其他配置信息"></a>其他配置信息</h2><h3 id="proxy-next-upstream"><a href="#proxy-next-upstream" class="headerlink" title="proxy_next_upstream"></a>proxy_next_upstream</h3><p>语法：<code>proxy_next_upstream [error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 |chttp_404 | off ];</code><br>默认：<code>proxy_next_upstream error timeout</code>;<br>配置块：<code>http、server、location</code></p>
<p>这个配置表示当向一台上有服务器转发请求出现错误的时候，继续换一台上游服务器来处理这个请求。默认情况下，上游服务器一旦开始发送响应数据，Nginx反向代理服务器会立刻把应答包转发给客户端。因此，一旦Nginx开始向客户端发送响应包，如果中途出现错误也不允许切换到下一个上有服务器继续处理的。这样做的目的是保证客户端只收到来自同一个上游服务器的应答。</p>
<h3 id="proxy-connect-timeout"><a href="#proxy-connect-timeout" class="headerlink" title="proxy_connect_timeout"></a>proxy_connect_timeout</h3><p>语法: <code>proxy_connect_timeout time</code>;<br>默认: <code>proxy_connect_timeout 60s</code>;<br>范围: <code>http, server, location</code></p>
<p>用于设置nginx与upstream server的连接超时时间，比如我们直接在location中设置proxy_connect_timeout 1ms， 1ms很短，如果无法在指定时间建立连接，就会报错。</p>
<h3 id="proxy-send-timeout"><a href="#proxy-send-timeout" class="headerlink" title="proxy_send_timeout"></a>proxy_send_timeout</h3><p>向后端写数据的超时时间，两次写操作的时间间隔如果大于这个值，也就是过了指定时间后端还没有收到数据，连接会被关闭</p>
<h3 id="proxy-read-timeout"><a href="#proxy-read-timeout" class="headerlink" title="proxy_read_timeout"></a>proxy_read_timeout</h3><p>从后端读取数据的超时时间，两次读取操作的时间间隔如果大于这个值，那么nginx和后端的链接会被关闭，如果一个请求的处理时间比较长，可以把这个值设置得大一些</p>
<h3 id="proxy-upstream-fail-timeout"><a href="#proxy-upstream-fail-timeout" class="headerlink" title="proxy_upstream_fail_timeout"></a>proxy_upstream_fail_timeout</h3><p>设置了某一个upstream后端失败了指定次数（max_fails）后，在fail_timeout时间内不再去请求它,默认为10秒<br>语法 <code>server address [fail_timeout=30s]</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">upstream backend &#123; #服务器集群名字</span><br><span class="line">    server 192.168.218.129:8080 weight&#x3D;1 max_fails&#x3D;2 fail_timeout&#x3D;600s;</span><br><span class="line">    server 192.168.218.131:8080 weight&#x3D;1 max_fails&#x3D;2 fail_timeout&#x3D;600s;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="nginx动静分离"><a href="#nginx动静分离" class="headerlink" title="nginx动静分离"></a>nginx动静分离</h2><p>动静分离<br>必须依赖服务器生存的称为动，不需要依赖容器的比如css/js或者图片等，这类就叫静</p>
<h3 id="静态资源的类型"><a href="#静态资源的类型" class="headerlink" title="静态资源的类型"></a>静态资源的类型</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line">types &#123;</span><br><span class="line">text&#x2F;html html htm shtml;</span><br><span class="line">text&#x2F;css css;</span><br><span class="line">text&#x2F;xml xml;</span><br><span class="line">image&#x2F;gif gif;</span><br><span class="line">image&#x2F;jpeg jpeg jpg;</span><br><span class="line">application&#x2F;javascript js;</span><br><span class="line">application&#x2F;atom+xml atom;</span><br><span class="line">application&#x2F;rss+xml rss;</span><br><span class="line">text&#x2F;mathml mml;</span><br><span class="line">text&#x2F;plain txt;</span><br><span class="line">text&#x2F;vnd.sun.j2me.app-descriptor jad;</span><br><span class="line">text&#x2F;vnd.wap.wml wml;</span><br><span class="line">text&#x2F;x-component htc;</span><br><span class="line">image&#x2F;png png;</span><br><span class="line">image&#x2F;svg+xml svg svgz;</span><br><span class="line">image&#x2F;tiff tif tiff;</span><br><span class="line">image&#x2F;vnd.wap.wbmp wbmp;</span><br><span class="line">image&#x2F;webp webp;</span><br><span class="line">image&#x2F;x-icon ico;</span><br><span class="line">image&#x2F;x-jng jng;</span><br><span class="line">image&#x2F;x-ms-bmp bmp;</span><br><span class="line">application&#x2F;font-woff woff;</span><br><span class="line">application&#x2F;java-archive jar war ear;</span><br><span class="line">application&#x2F;json json;</span><br><span class="line">application&#x2F;mac-binhex40 hqx;</span><br><span class="line">application&#x2F;msword doc;</span><br><span class="line">application&#x2F;pdf pdf;</span><br><span class="line">application&#x2F;postscript ps eps ai;</span><br><span class="line">application&#x2F;rtf rtf;</span><br><span class="line">application&#x2F;vnd.apple.mpegurl m3u8;</span><br><span class="line">application&#x2F;vnd.google-earth.kml+xml kml;</span><br><span class="line">application&#x2F;vnd.google-earth.kmz kmz;</span><br><span class="line">application&#x2F;vnd.ms-excel xls;</span><br><span class="line">application&#x2F;vnd.ms-fontobject eot;</span><br><span class="line">application&#x2F;vnd.ms-powerpoint ppt;</span><br><span class="line">application&#x2F;vnd.oasis.opendocument.graphics odg;</span><br><span class="line">application&#x2F;vnd.oasis.opendocument.presentation odp;</span><br><span class="line">application&#x2F;vnd.oasis.opendocument.spreadsheet ods;</span><br><span class="line">application&#x2F;vnd.oasis.opendocument.text odt;</span><br><span class="line">application&#x2F;vnd.openxmlformats-officedocument.presentationml.presentation pptx;</span><br><span class="line">application&#x2F;vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx;</span><br><span class="line">application&#x2F;vnd.openxmlformats-officedocument.wordprocessingml.document docx;</span><br><span class="line">application&#x2F;vnd.wap.wmlc wmlc;</span><br><span class="line">application&#x2F;x-7z-compressed 7z;</span><br><span class="line">application&#x2F;x-cocoa cco;</span><br><span class="line">application&#x2F;x-java-archive-diff jardiff;</span><br><span class="line">application&#x2F;x-java-jnlp-file jnlp;</span><br><span class="line">application&#x2F;x-makeself run;</span><br><span class="line">application&#x2F;x-perl pl pm;</span><br><span class="line">application&#x2F;x-pilot prc pdb;</span><br><span class="line">application&#x2F;x-rar-compressed rar;</span><br><span class="line">application&#x2F;x-redhat-package-manager rpm;</span><br><span class="line">application&#x2F;x-sea sea;</span><br><span class="line">application&#x2F;x-shockwave-flash swf;</span><br><span class="line">application&#x2F;x-stuffit sit;</span><br><span class="line">application&#x2F;x-tcl tcl tk;</span><br><span class="line">application&#x2F;x-x509-ca-cert der pem crt;</span><br><span class="line">application&#x2F;x-xpinstall xpi;</span><br><span class="line">application&#x2F;xhtml+xml xhtml;</span><br><span class="line">application&#x2F;xspf+xml xspf;</span><br><span class="line">application&#x2F;zip zip;</span><br><span class="line">application&#x2F;octet-stream bin exe dll;</span><br><span class="line">application&#x2F;octet-stream deb;</span><br><span class="line">application&#x2F;octet-stream dmg;</span><br><span class="line">application&#x2F;octet-stream iso img;</span><br><span class="line">application&#x2F;octet-stream msi msp msm;</span><br><span class="line">audio&#x2F;midi mid midi kar;</span><br><span class="line">audio&#x2F;mpeg mp3;</span><br><span class="line">audio&#x2F;ogg ogg;</span><br><span class="line">audio&#x2F;x-m4a m4a;</span><br><span class="line">audio&#x2F;x-realaudio ra;</span><br><span class="line">video&#x2F;3gpp 3gpp 3gp;</span><br><span class="line">video&#x2F;mp2t ts;</span><br><span class="line">video&#x2F;mp4 mp4;</span><br><span class="line">video&#x2F;mpeg mpeg mpg;</span><br><span class="line">video&#x2F;quicktime mov;</span><br><span class="line">video&#x2F;webm webm;</span><br><span class="line">video&#x2F;x-flv flv;</span><br><span class="line">video&#x2F;x-m4v m4v;</span><br><span class="line">video&#x2F;x-mng mng;</span><br><span class="line">video&#x2F;x-ms-asf asx asf;</span><br><span class="line">video&#x2F;x-ms-wmv wmv;</span><br><span class="line">video&#x2F;x-msvideo avi;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>在Nginx的conf目录下，有一个mime.types文件<br>用户访问一个网站，然后从服务器端获取相应的资源通过浏览器进行解析渲染最后展示给用户，而服务端可以返回各种类型的内容，比如xml、jpg、png、gif、flash、MP4、html、css等等，那么浏览器就是根据mime-type来决定用什么形式来展示的服务器返回的资源给到浏览器时，会把媒体类型告知浏览器，这个告知的标识就是Content-Type，比如ContentType:text/html。</p>
</blockquote>
<h3 id="动静分离的好处"><a href="#动静分离的好处" class="headerlink" title="动静分离的好处"></a>动静分离的好处</h3><ul>
<li>Nginx本身就是一个高性能的静态web服务器；</li>
<li>其实静态文件有一个特点就是基本上变化不大，所以动静分离以后可以对静态文件进行缓存、或者压缩提高网站性能</li>
</ul>
<h3 id="缓存"><a href="#缓存" class="headerlink" title="缓存"></a>缓存</h3><p>当一个客户端请求web服务器, 请求的内容可以从以下几个地方获取：服务器、浏览器缓存中或缓存服务器中。这取决于服务器端输出的页面信息</p>
<p>浏览器缓存将文件保存在客户端，好的缓存策略可以减少对网络带宽的占用，可以提高访问速度，提高用户的体验，还可以减轻服务器的负担nginx缓存配置</p>
<h3 id="nginx缓存配置"><a href="#nginx缓存配置" class="headerlink" title="nginx缓存配置"></a>nginx缓存配置</h3><p>nginx可以通过expires设置缓存，比如可以针对图片做缓存，因为图片这类信息基本上不会改变。<br>在location中设置expires<br>格式：<code>expires 30s|m|h|d</code><br>示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">location ~ .*.(jpg|jpeg|gif|bmp|png|js|css|ico)$ &#123;</span><br><span class="line">    root static;</span><br><span class="line">    expires 1d;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="压缩"><a href="#压缩" class="headerlink" title="压缩"></a>压缩</h3><p>一个网站一定会包含很多的静态文件，比如图片、脚本、样式等等，而这些css/js可能本身会比较大，那么在网络传输的时候就会比较慢，从而导致网站的渲染速度。因此Nginx中提供了一种Gzip的压缩优化手段，可以对后端的文件进行压缩传输，压缩以后的好处在于能够降低文件的大小来提高传输效率。</p>
<p>配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">gzip on|off 是否开启gzip压缩</span><br><span class="line">gzip_buffers 4 16k #设置gzip申请内存的大小，作用是按指定大小的倍数申请内存空间。4 16k代表按照原始数据大小以16k为单位的4倍申请内存。</span><br><span class="line">gzip_comp_level[1-9] 压缩级别， 级别越高，压缩越小，但是会占用CPU资源</span><br><span class="line">gzip_disable #正则匹配UA 表示什么样的浏览器不进行gzip</span><br><span class="line">gzip_min_length #开始压缩的最小长度（小于多少就不做压缩），可以指定单位，比如 1k</span><br><span class="line">gzip_http_version 1.0|1.1 表示开始压缩的http协议版本</span><br><span class="line">gzip_proxied （nginx 做前端代理时启用该选项，表示无论后端服务器的headers头返回什么信息，都无条件启用压缩）</span><br><span class="line">gzip_type text&#x2F;pliain,application&#x2F;xml 对那些类型的文件做压缩 （conf&#x2F;mime.conf）</span><br><span class="line">gzip_vary on|off 是否传输gzip压缩标识; 启用应答头&quot;Vary: Accept-Encoding&quot;;给代理服务器用的，有的浏览器支持压缩，有的不支持，所以避免浪费不支持的也压缩，所以根据客户端的HTTP头来判断，是否需要压缩</span><br></pre></td></tr></table></figure>
<p>示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">http &#123;</span><br><span class="line">    include mime.types;</span><br><span class="line">    default_type application&#x2F;octet-stream;</span><br><span class="line">    #log_format main &#39;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#39;</span><br><span class="line">                    # &#39;$status $body_bytes_sent &quot;$http_referer&quot; &#39;</span><br><span class="line">                    # &#39;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#39;;</span><br><span class="line">    #access_log logs&#x2F;access.log main;</span><br><span class="line">    sendfile on;</span><br><span class="line">    #tcp_nopush on;</span><br><span class="line">    keepalive_timeout 60;</span><br><span class="line">    include extra&#x2F;*.conf;</span><br><span class="line">    gzip on;</span><br><span class="line">    gzip_min_length 5k;</span><br><span class="line">    gzip_comp_level 3;</span><br><span class="line">    gzip_types application&#x2F;javascript image&#x2F;jpeg image&#x2F;svg+xml;</span><br><span class="line">    gzip_buffers 4 32k;</span><br><span class="line">    gzip_vary on;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="防盗链"><a href="#防盗链" class="headerlink" title="防盗链"></a>防盗链</h2><p>一个网站上会有很多的图片，如果你不希望其他网站直接用你的图片地址访问自己的图片，或者希望对图片有版权保护。再或者不希望被第三方调用造成服务器的负载以及消耗比较多的流量问题，那么防盗链就是你必须要做的</p>
<h3 id="防盗链配置"><a href="#防盗链配置" class="headerlink" title="防盗链配置"></a>防盗链配置</h3><p>语法: <code>valid_referers none | blocked | server_names | string ...</code>;<br>配置块: <code>server, location</code></p>
<p><code>Referer</code>请求头为指定值时，内嵌变量$invalid_referer被设置为空字符串，否则这个变量会被置成“1”。查找匹配时不区分大小写，其中none表示缺少referer请求头、blocked表示请求头存在，但是它的值被防火墙或者代理服务器删除、server_names表示referer请求头包含指定的虚拟主机名</p>
<p>示例代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">location ~ .*\.(js|css|png|svg|ico|jpg)$ &#123;</span><br><span class="line">    valid_referers none blocked 192.168.11.160;</span><br><span class="line">    if ($invalid_referer) &#123;</span><br><span class="line">        return 404;</span><br><span class="line">    &#125;</span><br><span class="line">    root static-resource;</span><br><span class="line">    expires 1d;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="跨域访问"><a href="#跨域访问" class="headerlink" title="跨域访问"></a>跨域访问</h2><p>如果两个节点的协议、域名、端口、子域名不同，那么进行的操作都是跨域的，浏览器为了安全问题都是限制跨域访问，所以跨域其实是浏览器本身的限制。</p>
<h3 id="解决办法"><a href="#解决办法" class="headerlink" title="解决办法"></a>解决办法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">server&#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    server_name localhost;</span><br><span class="line">    location &#x2F; &#123;</span><br><span class="line">        proxy_pass http:&#x2F;&#x2F;192.168.11.154:8080;</span><br><span class="line">        proxy_set_header Host $host;</span><br><span class="line">        proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">        proxy_send_timeout 60s;</span><br><span class="line">        proxy_read_timeout 60s;</span><br><span class="line">        proxy_connect_timeout 60s;</span><br><span class="line">        # 解决跨域问题</span><br><span class="line">        add_header &#39;Access-Control-Allow-Origin&#39; &#39;*&#39;;  #允许来自所有的访问地址</span><br><span class="line">        add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET,PUT,POST,DELETE,OPTIONS&#39;; #支持的请求方式</span><br><span class="line">        add_header &#39;Access-Control-Allow-Header&#39; &#39;Content-Type,*&#39;; #支持的媒体类型</span><br><span class="line">    &#125;</span><br><span class="line">    location ~ .*\.(gif|jpg|ico|png|css|svg|js)$ &#123;</span><br><span class="line">        root static;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>











 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://dxysun.com/2020/01/05/nginxForLearn2/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/nginx/" rel="tag">nginx</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/02/22/javaForDubbo1/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Dubbo学习笔记之基本使用
          
        </div>
      </a>
    
    
      <a href="/2020/01/04/nginxForLearn1/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">nginx学习之安装和配置</div>
      </a>
    
  </nav>

  
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2024
        <i class="ri-heart-fill heart_icon"></i> dxysun
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
        <li>
          <a href="https://beian.miit.gov.cn" target="_black" rel="nofollow">豫ICP备17012675号-1</a>
        </li>
        
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="https://dxysun.com/static/logo.png" alt="迎着朝阳"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/alipay-20201219151322.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/weixin-20201219151346.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>